/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.encryption;

/**
 *
 * @author Dao Thai Duy
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import sun.misc.CharacterEncoder;

public final class PasswordService
{
  private static PasswordService instance;

  private PasswordService()
  {
  }

  public synchronized String encrypt(String plaintext) throws UnsupportedEncodingException, NoSuchAlgorithmException
  {
    MessageDigest md = null;

    md = MessageDigest.getInstance("SHA"); 
    md.update(plaintext.getBytes("UTF-8")); 
    byte raw[] = md.digest(); 
    String hash = (new BASE64Encoder()).encode(raw); 
    return hash; 
  }
  
  public static synchronized PasswordService getInstance()
  {
    if(instance == null)
    {
       instance = new PasswordService(); 
    } 
    return instance;
  }
}